from api_setting import *
from dim_api_auto_test.DimAPI.core.request_client import RestClient
from tools.get_pass_day import get_pass_date
from tools.get_today import get_today_date


class Search(RestClient):
    def __init__(self, **kwargs):
        super(Search, self).__init__(**kwargs)

    def search_material(self, matCode):
        """
        根据物料编码查询物料主档
        :param matCode:
        :return:
        """
        url = SEARCH_MATERIAL_SEARCH_URL
        params = {"matCode": matCode}
        r = self.post(url=url, params=params)
        return r.content

    def search_supplier(self, supName):
        """
        供应商档案：按供应商名称查询
        :param supName:
        :return:
        """
        url = SUPPLIER_SEARCH_URL
        params = {"supName": supName, "status": "Effective"}
        r = self.post(url=url, params=params)
        return r.content

    def search_material_code_apply(self, orderNo):
        """
        物料编码申请：根据单号查询
        :param orderNo:
        :return:
        """
        url = MATERIAL_CODE_APPLY_SEARCH_URL
        params = {"orderNo": orderNo}
        r = self.post(url=url, params=params)
        return r.content

    def search_material_code_apply_by_status(self, status):
        """
        物料编码申请：根据单号查询
        :param status:
        :return:
        """
        url = MATERIAL_CODE_APPLY_SEARCH_URL
        params = {"state": status}
        r = self.post(url=url, params=params)
        return r.content

    def search_material_deal_bill(self, billNo):
        """
        物料主档处理：根据单号查询
        :param billNo:
        :return:
        """
        url = MATERIAL_DEAL_BILL_SEARCH_URL
        params = {"billNo": billNo}
        r = self.post(url=url, params=params)
        return r.content

    def search_purchase_contract(self, contractNo):
        """
        采购合同：根据单号查询
        :param contractNo:
        :return:
        """
        url = PURCHASE_CONTRACT_SEARCH_URL
        params = {"contractNo": contractNo}
        r = self.post(url=url, params=params)
        return r.content

    def search_purchase_contract_by_status(self, status):
        """
        采购合同：根据状态查询
        :param status:
        :return:
        """
        url = PURCHASE_CONTRACT_SEARCH_URL
        params = {"status": status}
        r = self.post(url=url, params=params)
        return r.content

    def search_purchase_contract_change(self, poContractNo):
        """
        合同变更审批：根据合同号查询
        :param poContractNo:
        :return:
        """
        url = PURCHASE_CONTRACT_CHANGE_SEARCH_URL
        params = {"poContractNo": poContractNo}
        r = self.post(url=url, params=params)
        return r.content

    def search_purchase_contract_detail(self, matCode):
        """
        合同明细查询：根据物料编码查询
        :param matCode:
        :return:
        """
        url = PURCHASE_CONTRACT_DETAIL_SEARCH_URL
        params = {"matCode": matCode}
        r = self.post(url=url, params=params)
        return r.content

    def search_purchase_apply_detail(self, hrname):
        """
        待处理请求查询：根据采购员查询
        :param hrname:
        :return:
        """
        url = PURCHASE_APPLY_DETAIL_SEARCH_URL
        params = {
            "detailsStatus": "WaitPurchase",
            "hrname": hrname,
            "isContract": "ALL",
            "applied": "ALL",
            "endDate": get_today_date(),
            "beginDate": get_pass_date(30),
        }
        r = self.post(url=url, params=params)
        return r.content

    def search_purchase_order(self, purchaseUserName):
        """
        采购订单：根据采购员查询
        :param purchaseUserName:
        :return:
        """
        url = PURCHASE_ORDER_SEARCH_URL
        params = {
            "status": "Draft",
            "orderDateStart": get_pass_date(90),
            "orderDateEnd": get_today_date(),
            "purchaseUserName": purchaseUserName,
        }
        r = self.post(url=url, params=params)
        return r.content

    def search_purchase_order_detail(self, b2bStatus):
        """
        订单跟进：根据B2B状态查询
        :param b2bStatus:
        :return:
        """
        url = PURCHASE_ORDER_DETAIL_SEARCH_URL
        params = {
            "b2bStatus": b2bStatus,
            "orderDateStart": get_pass_date(30),
            "orderDateEnd": get_today_date(),
            "purchaseUserName": "梁伟豪",
        }
        r = self.post(url=url, params=params)
        return r.content

    def search_wait_quote_list(self, hrname):
        """
        询报价处理-待报价页签：根据采购员查询
        :param hrname:
        :return:
        """
        url = WAIT_QUOTE_LIST_SEARCH_URL
        params = {
            "tags": "红",
            "inquiryProgress": "inactive",
            "hrname": hrname,
            "detailsStatus": "WaitPurchase",
        }
        r = self.post(url=url, params=params)
        return r.content

    def search_enquiry_price_bill(self, hrname):
        """
        询报价处理-比价初审明细查询
        :param hrname:
        :return:
        """
        url = ENQUIRY_PRICE_BILL_SEARCH_URL
        params = {"hrname": hrname, "tabNames": "FIRST_COMPARE_PRICE"}
        r = self.post(url=url, params=params)
        return r.content

    def search_enquiry_price_bill_deal(self, enquiryStatus):
        """
        比价处理：按询价状态查询
        :param enquiryStatus:
        :return:
        """
        url = ENQUIRY_PRICE_BILL_DEAL_SEARCH_URL
        params = {"enquiryStatus": enquiryStatus}
        r = self.post(url=url, params=params)
        return r.content

    def search_bid_tender(self):
        """
        招标大厅：查询所有的招标书
        :return:
        """
        url = BID_TENDER_SEARCH_URL
        r = self.post(url=url)
        return r.content

    def search_backlog_order(self, supCode):
        """
        待发货订单：根据供应商编码查询
        :param supCode:
        :return:
        """
        url = BACKLOG_ORDER_SEARCH_URL
        params = {
            "b2bStatus": "Received",
            "status": "Reviewed, Partial",
            "supCode": supCode,
        }
        r = self.post(url=url, params=params)
        return r.content

    def search_pu_invoice_list(self, supCode, status):
        """
        发货单查询：根据供应商编码、状态查询
        :param supCode:
        :param status:
        :return:
        """
        url = PU_INVOICE_LIST_SEARCH_URL
        params = {"supCode": supCode, "status": status}
        r = self.post(url=url, params=params)
        return r.content

    def search_supplier_bill(self, billStatus):
        """
        供应商档案处理单据：按状态查询
        :param billStatus:
        :return:
        """
        url = SUPPLIER_BILL_SEARCH
        params = {"billStatus": billStatus}
        r = self.post(url=url, params=params)
        return r.content


if __name__ == "__main__":
    test = Search()
    # print(result := test.search_material("14500"))
    # result = test.search_supplier("班本")
    # result = test.search_material_code_apply("MA2112070045")
    result = test.search_purchase_contract_by_status("Draft")
    print(result["data"]["list"][0])
